NPU_MAC_DICT = {
    "171.60.1.101": "192.168.111.101-192.168.111.108",
    "171.60.1.102": "192.168.111.109-192.168.111.116",
    "171.60.1.103": "192.168.111.117-192.168.111.124",
    "171.60.1.104": "192.168.111.125-192.168.111.132",
    
    "171.60.1.105": "192.168.112.101-192.168.112.108",
    "171.60.1.106": "192.168.112.109-192.168.112.116",
    
    "171.60.1.107": "192.168.113.101-192.168.113.108",
    "171.60.1.108": "192.168.113.109-192.168.113.116",
    "171.60.1.109": "192.168.113.117-192.168.113.124",
    "171.60.1.110": "192.168.113.125-192.168.113.132",
    
    "171.60.1.111": "192.168.114.101-192.168.114.108",
    "171.60.1.112": "192.168.114.109-192.168.114.116",
    "171.60.1.113": "192.168.114.117-192.168.114.124",
    "171.60.1.114": "192.168.114.125-192.168.114.132",
}

NPU_Gateway = {
    "171.60.1.101": "192.168.111.1",
    "171.60.1.102": "192.168.111.1",
    "171.60.1.103": "192.168.111.1",
    "171.60.1.104": "192.168.111.1",
    
    "171.60.1.105": "192.168.112.1",
    "171.60.1.106": "192.168.112.1",
    
    "171.60.1.107": "192.168.113.1",
    "171.60.1.108": "192.168.113.1",
    "171.60.1.109": "192.168.113.1",
    "171.60.1.110": "192.168.113.1",
    
    "171.60.1.111": "192.168.114.1",
    "171.60.1.112": "192.168.114.1",
    "171.60.1.113": "192.168.114.1",
    "171.60.1.114": "192.168.114.1",
}

import subprocess

def get_ip_address(interface_name):
    try:
        result = subprocess.check_output(['ifconfig', interface_name]).decode('utf-8')
        ip_index = result.find('inet')
        if ip_index != -1:
            ip = result[ip_index + 5:result.find(' ', ip_index + 5 )]
            return ip
        else:
            return -1 
    except:
        return -1

def run_cmd(cmd):
    try:
        result = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
        print(f"run cmd: {cmd}")
        return result.decode('utf-8')
    except subprocess.CalledProcessError as e:
        return f"failed to run cmd: {cmd}, reason: {e.output.decode('utf-8')}"
    
def ip_range(start_ip, end_ip):
    start = list(map(int, start_ip.split('.')))
    end = list(map(int, end_ip.split('.')))
    current = start
    while current <= end:
        yield '.'.join(map(str, current))
        current[3] += 1
        for i in (3, 2, 1):
            if current[i] == 256:
                current[i] = 0
                current[i - 1] += 1
        if current[0] == 256:
            break
                

class hccn_tool():
    def __init__(self, config):
        run_cmd("hccn_tool --help")
    
    @staticmethod
    def set_npu_ip():
        ip = get_ip_address('enp61s0f0')
        NPU_IP_RANGE = NPU_MAC_DICT[ip]
        npu_gateway =NPU_Gateway[ip]
        start_ip = NPU_IP_RANGE.split("-")[0]
        end_ip= NPU_IP_RANGE.split("-")[1]
        count = 0
        for ip in ip_range(start_ip, end_ip):
            run_cmd(f'hccn_tool -i {count} -ip -s address {ip} netmask 255.255.255.0')
            run_cmd(f'hccn_tool -i {count} -gateway -s gateway {npu_gateway} ')
            run_cmd(f'hccn_tool -i {count}  -netdetect -s address {npu_gateway} ')
            count = count + 1
hccn_tool.set_npu_ip()
